package com.donleo.alipay.controller;

import com.donleo.alipay.model.PayLog;
import com.donleo.alipay.model.RefundLog;
import com.donleo.alipay.service.PayLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 支付记录
 *
 * @author liangd
 * @since 2021-03-05 15:54
 */
@Slf4j
@RestController
@RequestMapping("/alipay")
public class PayLogController {

    @Autowired
    private PayLogService payLogService;

    /**
     * 支付接口调用
     * alipay.trade.page.pay
     *
     * @param price    价格
     * @param name     商品名字
     * @param response 响应
     */
    @GetMapping("/pay")
    public void pay(@RequestParam String price, @RequestParam String name, HttpServletResponse response) {
        try {
            payLogService.pay(price, name, response);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }

    /**
     * 服务器异步通知
     *
     * @param request 请求
     * @return String
     */
    @PostMapping("/notifyPay")
    public String notifyPay(HttpServletRequest request) {
        try {
            return payLogService.notifyPay(request);
        } catch (Exception e) {
            log.error(e.getMessage());
            return "fail";
        }
    }

    /**
     * 支付成功后的跳转页面
     */
    @GetMapping("/returnPay")
    public String returnPay() {
        return "支付成功";
    }

    /**
     * 交易查询
     *
     * @param request HttpServletRequest
     * @return Object
     */
    @GetMapping("/queryPayLog")
    public String queryPayLog(HttpServletRequest request) {
        try {
            return payLogService.queryPayLog(request);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 关闭交易接口
     *
     * @return String
     */
    @GetMapping("/closePayLog")
    public String closePayLog(HttpServletRequest request) {
        try {
            return payLogService.closePayLog(request);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 退款接口(支持部分退款)
     *
     * @param request HttpServletRequest
     * @return String
     */
    @GetMapping("/refundPay")
    public String refundPay(HttpServletRequest request) {
        try {
            return payLogService.refundPay(request);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 退款记录查询
     *
     * @param request HttpServletRequest
     * @return String
     */
    @GetMapping("/refundQuery")
    public String refundQuery(HttpServletRequest request) {
        try {
            return payLogService.refundQuery(request);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 账单查询
     *
     * @param request HttpServletRequest
     * @return String
     */
    @GetMapping("/downloadPayLog")
    public String downloadPayLog(HttpServletRequest request) {
        try {
            return payLogService.downloadPayLog(request);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 查询所有交易记录
     *
     * @return List<PayLog>
     */
    @GetMapping("/queryAllPayLog")
    public List<PayLog> queryAllPayLog() {
        try {
            return payLogService.list();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    /**
     * 本地退款记录查询
     *
     * @return List<RefundLog>
     */
    @GetMapping("/localPayLogQuery")
    public List<RefundLog> localPayLogQuery() {
        try {
            return payLogService.localPayLogQuery();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }
}
